/*
 * Copyright (C) 2012 artit91
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.entities.eventhandlers;

import java.util.Random;
import java.util.logging.Logger;
import rpg.entities.Sprite;
import rpg.entities.factories.CritterFactory;
import rpg.entities.factories.CreatureFactory;
import rpg.entities.factories.SpriteFactory;
import rpg.entities.properties.CreatureProperty;
import rpg.gfx.Bitmap;

/**
 *
 * @author artit91
 */
public class PeacefulRandomMovement extends EventHandler {
 
    private int animDelay = 5;
    private int animState = 0;
    private int nextRandom = 20;
    private int dir = 0;
    private SpriteFactory ef;
    private Random rnd;
    
    public PeacefulRandomMovement(final SpriteFactory ef) {
        this.ef = ef;
	rnd = new Random();
    }
    
    @Override
    public void tick() {

        animDelay--;
        if (animDelay <= 0) {
		--nextRandom;
                animDelay = 5;
                animState++;
                animState %= 3;
		
        }
	if(nextRandom <= 0){
		nextRandom = 18+rnd.nextInt(10);
		((CreatureProperty)ef.getProp()).setNextMove(ef.getProp().getLocation().getPossibleLocation(ef.getMap()));
	}
        int moveDir = ((CreatureProperty)ef.getProp()).getNextMove().getDir(ef.getProp().getLocation());
        
        if(moveDir != -1){
                dir = moveDir;
                if(!ef.getProp().getLocation().move(moveDir, 1)){
                        ((CreatureProperty)ef.getProp()).setNextMove(ef.getProp().getLocation().getPossibleLocation(ef.getMap()));
                }
        } else {
                Random rand = new Random();
                if(rand.nextBoolean()){
                        ((CreatureProperty)ef.getProp()).setNextMove(ef.getProp().getLocation().getPossibleLocation(ef.getMap()));
                }
        }
    }
    
    @Override
    public boolean damage(Sprite causer, int dmg) {
            if (ef instanceof CritterFactory) {
                return true;
            }
            else {
                return ((CreatureFactory)ef).damage(causer, dmg);
            }
    }
    
    @Override
    public Bitmap getDrawGraphics() {
	return ef.bitmaps[animState % 3 + dir * 3];
    }
    private static final Logger LOG = Logger.getLogger(PeacefulRandomMovement.class.getName());
}
